package com.tencent.iov.cowin.befsale.clue.controller.cus.assembler;

import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddCustomerFollowPlanRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateCustomerFollowPlanRequest;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.DealerCustomerFollowPlan;
import com.tencent.iov.cowin.befsale.clue.value.enums.FollowPlanStatusEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.PlanFollowTimeCodeEnum;
import com.software.boot.common.model.dao.BaseAssembler;
import com.software.boot.common.model.rest.BaseRequest;
import com.software.boot.common.model.rest.BaseResponse;
import com.software.boot.common.util.DateUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.DeleteEnum;
import com.software.common.enums.SCRMRequestHeaderEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.util.Date;

/**
 * @Author: xiamu
 * @Date: 2021-12-21 17:29:56
 * @Description: 经销商跟进线索表适配器，负责vo<->entity、vo<->po的转换和必要的参数检查，行为
 */
@Slf4j
public class DealerCustomerFollowPlanAssembler extends BaseAssembler<BaseResponse, BaseRequest, DealerCustomerFollowPlan> {

    private final static Integer DATE_NUM = 6;

    public static void initAddDealerCustomerFollowPlanRequest(AddCustomerFollowPlanRequest request) {
        if (EmptyUtils.isEmpty(request.getCorpId())) {
            request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getTenantId())) {
            request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedBy())) {
            request.setCreatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedBy())) {
            request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedName())) {
            request.setCreatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedName())) {
            request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getDealerId())) {
            request.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedTime())) {
            request.setCreatedTime(new Date());
        }
        if (EmptyUtils.isEmpty(request.getUpdatedTime())) {
            request.setUpdatedTime(new Date());
        }
        if (EmptyUtils.isEmpty(request.getStatus())) {
            request.setStatus(FollowPlanStatusEnum.WAIT_FOLLOW.getCode());
        }
        request.setDeleted(DeleteEnum.INIT.getFlag());
    }

    public static UpdateCustomerFollowPlanRequest initUpdateDealerCustomerFollowPlanRequest(UpdateCustomerFollowPlanRequest request) {
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    /**
     * 填充计划跟进时间
     * @param request 参数
     */
    public static void initFollowPlanTime(AddCustomerFollowPlanRequest request) {
        if (EmptyUtils.isEmpty(request.getPlanFollowTimeCode())) {
            return;
        }
        PlanFollowTimeCodeEnum planFollowTimeCodeEnum = PlanFollowTimeCodeEnum.getPlanFollowTimeCodeEnum(request.getPlanFollowTimeCode());
        if (EmptyUtils.isEmpty(planFollowTimeCodeEnum)) {
            return;
        }

        ValidateUtils.checkObjNull(planFollowTimeCodeEnum, "选择计划跟进时间错误");
        String dateTime = DateUtil.toDateString(new Date(), DateUtil.DEFAULT_DATE_PATTERN);
        dateTime = StringUtils.join(dateTime, " 23:59:59");
        Date date = DateUtil.toDate(dateTime, DateUtil.DEFAULT_DATETIME_PATTERN);
        switch (planFollowTimeCodeEnum) {
            case FOLLOW_TIME_CODE_DAY:
            case FOLLOW_TIME_CODE_THIRD_DAY:
            case FOLLOW_TIME_CODE_WEEK_DAY:
                request.setPlanFollowTime(DateUtil.addDate(date, planFollowTimeCodeEnum.getDay()));
                break;
            case FOLLOW_TIME_CODE_IN_WEEK:
                DateTime dt = new DateTime(date);
                request.setPlanFollowTime(DateUtil.addDate(date, (DATE_NUM - dt.getDayOfWeek())));
                break;
            default:
                break;
        }

    }
}
